package com.ytf.test.eighth;

class Characteristic{
    private String s;

    public Characteristic(String s) {
        this.s = s;
        System.out.println("Create Characteristic "+ s);
    }

    protected void dispose(){
        System.out.println("disposing Characteristic "+s);
    }
}

class Description{
    private String s;

    public Description(String s) {
        this.s = s;
        System.out.println("Create Description "+ s);
    }

    protected void dispose(){
        System.out.println("disposing Description "+s);
    }
}

class LivingCreature{
    private Characteristic characteristic = new Characteristic("is alive");
    private Description description = new Description("Basic living Creature");

    public LivingCreature() {
        System.out.println("LivingCreature()");
    }

    protected void dispose(){
        System.out.println("livingCreature dispose");
        description.dispose();
        characteristic.dispose();
    }
}
class Animal extends LivingCreature{
    private Characteristic characteristic = new Characteristic("has heart");
    private Description description = new Description("Animal not Vegetable");

    public Animal() {
        System.out.println("Animal()");
    }

    @Override
    protected void dispose(){
        System.out.println("Animal dispose");
        description.dispose();
        characteristic.dispose();
        super.dispose();
    }
}
class Amphibian extends Animal{
    private Characteristic characteristic = new Characteristic("can live water");
    private Description description = new Description("both water and land");

    public Amphibian() {
        System.out.println("Amphibian()");
    }

    @Override
    protected void dispose(){
        System.out.println("Amphibian dispose");
        description.dispose();
        characteristic.dispose();
        super.dispose();
    }
}

/**
 * @author: YTF
 * @date: 2025/1/24 17:49
 * @version: 1.0
 * @description:
 */
public class Frog extends Amphibian{
    private Characteristic characteristic = new Characteristic("croaks");
    private Description description = new Description("Eats Bugs");

    public Frog() {
        System.out.println("Frog()");
    }

    @Override
    protected void dispose(){
        System.out.println("Frog dispose");
        description.dispose();
        characteristic.dispose();
        super.dispose();
    }

    public static void main(String[] args) {
        Frog frog = new Frog();
        System.out.println("Bye!");
        frog.dispose();

        /**
         * OutPut:
         * Create Characteristic is alive
         * Create Description Basic living Creature
         * LivingCreature()
         * Create Characteristic has heart
         * Create Description Animal not Vegetable
         * Animal()
         * Create Characteristic can live water
         * Create Description both water and land
         * Amphibian()
         * Create Characteristic croaks
         * Create Description Eats Bugs
         * Frog()
         * Bye!
         * Frog dispose
         * disposing Description Eats Bugs
         * disposing Characteristic croaks
         * Amphibian dispose
         * disposing Description both water and land
         * disposing Characteristic can live water
         * Animal dispose
         * disposing Description Animal not Vegetable
         * disposing Characteristic has heart
         * livingCreature dispose
         * disposing Description Basic living Creature
         * disposing Characteristic is alive
         *
         * 总结：
         * 初始化是顺序，销毁是倒序。
         */
    }
}
